package com.getpebble.android.framework.protocol.inbound;

import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.util.ByteUtils;
import com.google.common.primitives.UnsignedInteger;
import com.sun.mail.smtp.SMTPMessage;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;

public class PblInboundExtensibleNotificationMessage extends PblInboundMessage {
    private static final String TAG = PblInboundExtensibleNotificationMessage.class.getSimpleName();
    private InboundAckNack mAckNack;
    private int mActionId;
    private Map<Integer, String> mAttributes = null;
    private InboundNotificationMessageType mMessageType;
    private UnsignedInteger mNextAvailableId;
    private UnsignedInteger mNotificationId;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundExtensibleNotificationMessage$InboundNotificationMessageType = new int[InboundNotificationMessageType.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundExtensibleNotificationMessage$InboundNotificationMessageType[InboundNotificationMessageType.INVOKE_ACTION.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundExtensibleNotificationMessage$InboundNotificationMessageType[InboundNotificationMessageType.ACK_NOTIFICATION.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
        }
    }

    public enum InboundAckNack {
        ACK(0),
        NACK_NOTIFICATION_ID_COLLISION(1),
        NACK_RETRY_NOTIFICATION(2),
        NACK_INVALID_DATA(3),
        ERROR(-1);
        
        private final int id;

        private InboundAckNack(int id) {
            this.id = id;
        }

        public int getId() {
            return this.id;
        }

        public static InboundAckNack fromId(int id) {
            for (InboundAckNack ackNack : values()) {
                if (ackNack.getId() == id) {
                    return ackNack;
                }
            }
            return ERROR;
        }
    }

    public enum InboundNotificationMessageType {
        INVOKE_ACTION(2),
        ACK_NOTIFICATION(16),
        ERROR(-1);
        
        private final int id;

        private InboundNotificationMessageType(int id) {
            this.id = id;
        }

        public int getId() {
            return this.id;
        }

        public static InboundNotificationMessageType fromId(int id) {
            for (InboundNotificationMessageType messageType : values()) {
                if (messageType.getId() == id) {
                    return messageType;
                }
            }
            return ERROR;
        }
    }

    public PblInboundExtensibleNotificationMessage(ProtocolMessage message) {
        super(message);
        ByteBuffer buffer = message.getDataBuffer();
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        this.mMessageType = InboundNotificationMessageType.fromId(buffer.get());
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundExtensibleNotificationMessage$InboundNotificationMessageType[this.mMessageType.ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                this.mNotificationId = ByteUtils.getUint32FromBuffer(buffer);
                this.mActionId = buffer.get();
                if (buffer.hasRemaining()) {
                    int numAttributes = ByteUtils.getUint8FromBuffer(buffer).intValue();
                    this.mAttributes = new HashMap();
                    for (int i = 0; i < numAttributes; i++) {
                        int attributeId = ByteUtils.getUint8FromBuffer(buffer).intValue();
                        String attribute = ByteUtils.getStringFromBuffer(buffer, ByteUtils.getUint16FromBuffer(buffer).intValue());
                        Trace.debug(TAG, String.format("Adding attribute ID=<%d> Content=<%s>", new Object[]{Integer.valueOf(attributeId), attribute}));
                        this.mAttributes.put(Integer.valueOf(attributeId), attribute);
                    }
                    return;
                }
                Trace.debug(TAG, "No attributes associated with INVOKE_ACTION response");
                return;
            case SMTPMessage.RETURN_HDRS /*2*/:
                this.mNotificationId = ByteUtils.getUint32FromBuffer(buffer);
                this.mAckNack = InboundAckNack.fromId(buffer.get());
                if (InboundAckNack.NACK_NOTIFICATION_ID_COLLISION.equals(this.mAckNack) && buffer.hasRemaining()) {
                    this.mNextAvailableId = ByteUtils.getUint32FromBuffer(buffer);
                    return;
                }
                return;
            default:
                return;
        }
    }

    public Map<Integer, String> getAttributes() {
        return this.mAttributes;
    }

    public InboundNotificationMessageType getMessageType() {
        return this.mMessageType;
    }

    public UnsignedInteger getNotificationId() {
        return this.mNotificationId;
    }

    public int getActionId() {
        return this.mActionId;
    }

    public InboundAckNack getAckNack() {
        return this.mAckNack;
    }

    public UnsignedInteger getNextAvailableId() {
        return this.mNextAvailableId;
    }

    EndpointId getId() {
        return EndpointId.EXTENSIBLE_NOTIFICATION;
    }

    protected int getMinSize() {
        return 6;
    }
}
